﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Xodeuz.SP.Common
{
    public class SPFolderHelper
    {
        public static void EnsureFolder(string listTitle, string folderName)
        {
            SPList list = SPContext.Current.Web.Lists.TryGetList(listTitle);

            if (list == null) throw new ArgumentNullException("SPList with the title: " + listTitle + " was not found.");

            if (!list.RootFolder.SubFolders.Cast<SPFolder>().Any(x => x.Exists && x.Name == folderName))
            {
                SPListItem newFolder = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
                newFolder.Update();
            }
        }
    }
}
